#include "log.h"

static lv_obj_t *ta1, *ta2;
static lv_obj_t * msgbox;
const char * btns[] = {""};
static lv_ft_info_t info1;
static lv_style_t style1;
static lv_ft_info_t info2;
static lv_style_t style2;
static lv_obj_t * kb;

void login(lv_event_t * e)
{
    const char * usrname  = lv_textarea_get_text(ta1);
    const char * password = lv_textarea_get_text(ta2);
    if(strcmp(usrname, "") == 0 && strcmp(password, "") == 0) {
        page2_show();
        lv_obj_del(page3);
    } else {
        msgbox = lv_msgbox_create(lv_scr_act(), "错误", "用户名或密码不正确", btns, true);
        lv_obj_center(msgbox);
        lv_obj_t * title   = lv_msgbox_get_title(msgbox);
        lv_obj_t * content = lv_msgbox_get_text(msgbox);
        if(title) lv_obj_set_style_text_font(title, info1.font, 0);
        if(content) lv_obj_set_style_text_font(content, info1.font, 0);
    }
}

void log_quit(lv_event_t * e)
{
    page1_show();
    lv_obj_del(page3);
}

void name_cb(lv_event_t * e)
{
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN); // 取消隐藏
    lv_keyboard_set_textarea(kb, ta1);
}

void passwd_cb(lv_event_t * e)
{
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN); // 取消隐藏;
    lv_keyboard_set_textarea(kb, ta2);
}

void blank_cb(lv_event_t * e)
{
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
}

void log_show()
{

    /*FreeType uses C standard file system, so no driver letter is required.*/
    info1.name   = "/font/simkai.ttf";
    info1.weight = 32;
    info1.style  = FT_FONT_STYLE_NORMAL;
    info1.mem    = NULL;
    if(!lv_ft_font_init(&info1)) {
        LV_LOG_ERROR("create failed.");
    }
    /*Create style with the new font*/

    lv_style_init(&style1);
    lv_style_set_text_font(&style1, info1.font);
    lv_style_set_text_align(&style1, LV_TEXT_ALIGN_CENTER);

    /*FreeType uses C standard file system, so no driver letter is required.*/
    info2.name   = "/font/simkai.ttf";
    info2.weight = 24;
    info2.style  = FT_FONT_STYLE_NORMAL;
    info2.mem    = NULL;
    if(!lv_ft_font_init(&info2)) {
        LV_LOG_ERROR("create failed.");
    }
    /*Create style with the new font*/

    lv_style_init(&style2);
    lv_style_set_text_font(&style2, info2.font);
    lv_style_set_text_align(&style2, LV_TEXT_ALIGN_CENTER);

    show_page(&page3);

    show_label(page3, 300, 100, 250, 20, &style1, "管理员登录系统");

    show_label(page3, 100, 50, 250, 150, &style2, "用户名：");

    show_label(page3, 100, 50, 250, 210, &style2, "密码：");

    ta1 = lv_textarea_create(page3);
    lv_textarea_set_one_line(ta1, true);
    lv_obj_set_size(ta1, 200, 50);
    lv_obj_set_pos(ta1, 350, 140);
    lv_textarea_set_placeholder_text(ta1, "username");
    lv_textarea_set_max_length(ta1, 32);
    lv_obj_add_event_cb(ta1, name_cb, LV_EVENT_FOCUSED, NULL);

    ta2 = lv_textarea_create(page3);
    lv_textarea_set_one_line(ta2, true);
    lv_obj_set_size(ta2, 200, 50);
    lv_obj_set_pos(ta2, 350, 200);
    lv_textarea_set_password_mode(ta2, true);
    lv_textarea_set_password_show_time(ta2, 10);
    lv_textarea_set_placeholder_text(ta2, "password");
    lv_textarea_set_max_length(ta2, 32);
    lv_obj_add_event_cb(ta2, passwd_cb, LV_EVENT_FOCUSED, NULL);

    lv_obj_t * btn1 = show_btn_label(page3, 80, 50, 300, 300, &style1, "登录");
    lv_obj_add_event_cb(btn1, login, LV_EVENT_CLICKED, NULL);

    lv_obj_t * btn2 = show_btn_label(page3, 80, 50, 400, 300, &style1, "返回");
    lv_obj_add_event_cb(btn2, log_quit, LV_EVENT_CLICKED, NULL);

    kb = lv_keyboard_create(page3);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 隐藏对象
    lv_obj_add_event_cb(page3, blank_cb, LV_EVENT_CLICKED, NULL);
}